﻿Imports System.Net
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
Imports System.IO
Imports System.IO.Compression

Namespace http

    Public Class HttpUtil

        '<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
        Public Shared Function GetHiddenValueFormName(ByVal content As String, ByVal findName As String) As String

            Dim rs As String = ""
            Dim patten As String = "<input[\s\S]+?type=""hidden""[\s\S]+?name=""" & findName & """[\s\S]+?value=(?<Value>[\s\S]+?)/>"
            Dim reg As New Regex(patten, RegexOptions.IgnoreCase)
            Dim mc As Match = reg.Match(content)
            If mc IsNot Nothing Then
                rs = mc.Groups("Value").Value
                rs = rs.Trim

                If rs.StartsWith("""") Then
                    rs = rs.Substring(1)
                End If

                If rs.EndsWith("""") Then
                    rs = rs.Substring(0, rs.Length - 1)
                End If
            End If

            Return rs

        End Function

#Region "读取响应的内容 进行解码"
        Public Shared Function ReadStreamEncodingContent(ByVal wbres As HttpWebResponse) As String

            If wbres Is Nothing Then
                Return ""
            End If

            Dim html As String
            Try
                If wbres.ContentEncoding.ToLower.Contains("gzip") Then
                    Dim responseStream As GZipStream = New GZipStream(wbres.GetResponseStream(), CompressionMode.Decompress)
                    Dim streamReader As StreamReader = New StreamReader(responseStream)
                    html = streamReader.ReadToEnd()
                    responseStream.Close()
                    streamReader.Close()
                ElseIf wbres.ContentEncoding.ToLower.Contains("deflate") Then
                    Dim responseStream As DeflateStream = New DeflateStream(wbres.GetResponseStream(), CompressionMode.Decompress)
                    Dim streamReader As StreamReader = New StreamReader(responseStream)
                    html = streamReader.ReadToEnd()
                    responseStream.Close()
                    streamReader.Close()
                Else
                    Dim streamReader As StreamReader = New StreamReader(wbres.GetResponseStream())
                    html = streamReader.ReadToEnd()
                    streamReader.Close()
                End If

                Return html

            Catch ex As Exception
                Return ""
            End Try

            Return ""

        End Function
#End Region

    End Class

End Namespace

